#include "stm32f1xx_hal.h"
#include "stm32f1xx_hal_gpio.h"
#define u8  uint8_t
#define u16 uint16_t
#define u32 uint32_t
/*------------------位绑定公式-----------------------------*/
/*														   */
/*片内地址=0x22000000+[(寄存器地址-0x20000000)x8+管脚号]x4 */
/*片外地址=0x42000000+[(寄存器地址-0x40000000)x8+管脚号]x4 */
/*合并后的地址如下：还是不明白？视乎少了一项			   */
#define BITBAND(addr, bitnum)  *((volatile unsigned long *)((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))) 

/*------------------寄存器地址-----------------------*/
/*													 */
/*		寄存器地址=寄存器基地址+寄存器偏移地址		 */
/*---------------------------------------------------*/
#define GPIOA_ODR_Addr    (GPIOA_BASE+0X0C) //0x4001080C 
#define GPIOB_ODR_Addr    (GPIOB_BASE+0X0C) //0x40010C0C 
#define GPIOC_ODR_Addr    (GPIOC_BASE+0X0C) //0x4001100C 
#define GPIOD_ODR_Addr    (GPIOD_BASE+0X0C) //0x4001140C 
#define GPIOE_ODR_Addr    (GPIOE_BASE+0X0C) //0x4001180C 
#define GPIOF_ODR_Addr    (GPIOF_BASE+0X0C) //0x40011A0C    
#define GPIOG_ODR_Addr    (GPIOG_BASE+0X0C) //0x40011E0C    

#define GPIOA_IDR_Addr    (GPIOA_BASE+0X08) //0x40010808 
#define GPIOB_IDR_Addr    (GPIOB_BASE+0X08) //0x40010C08 
#define GPIOC_IDR_Addr    (GPIOC_BASE+0X08) //0x40011008 
#define GPIOD_IDR_Addr    (GPIOD_BASE+0X08) //0x40011408 
#define GPIOE_IDR_Addr    (GPIOE_BASE+0X08) //0x40011808 
#define GPIOF_IDR_Addr    (GPIOF_BASE+0X08) //0x40011A08 
#define GPIOG_IDR_Addr    (GPIOG_BASE+0X08) //0x40011E08 

/*--------------------定义管脚输入/输出-------------------------*/
#define PAout(n)  BITBAND(GPIOA_ODR_Addr, n)
#define PAint(n)  BITBAND(GPIOA_IDR_Addr, n)
#define PBout(n)  BITBAND(GPIOB_ODR_Addr, n)
#define PBint(n)  BITBAND(GPIOB_IDR_Addr, n)
#define PCout(n)  BITBAND(GPIOC_ODR_Addr, n)
#define PCint(n)  BITBAND(GPIOC_IDR_Addr, n)
#define PDout(n)  BITBAND(GPIOD_ODR_Addr, n)
#define PDint(n)  BITBAND(GPIOD_IDR_Addr, n)
#define PEout(n)  BITBAND(GPIOE_ODR_Addr, n)
#define PEint(n)  BITBAND(GPIOE_IDR_Addr, n)
#define PFout(n)  BITBAND(GPIOF_ODR_Addr,n)  //输出 
#define PFint(n)  BITBAND(GPIOF_IDR_Addr,n)  //输入
#define PGout(n)  BITBAND(GPIOG_ODR_Addr,n)  //输出 
#define PGint(n)  BITBAND(GPIOG_IDR_Addr,n)  //输入
 
typedef enum{ 
  CMD_READ_SERIALNBR  = 0x3780, // read serial number 
  CMD_READ_STATUS     = 0xF32D, // read status register 
  CMD_CLEAR_STATUS    = 0x3041, // clear status register 
  CMD_HEATER_ENABLE   = 0x306D, // enabled heater 
  CMD_HEATER_DISABLE  = 0x3066, // disable heater 
  CMD_SOFT_RESET      = 0x30A2, // soft reset 
  CMD_MEAS_CLOCKSTR_H = 0x2C06, // measurement: clock stretching, high repeatability 
  CMD_MEAS_CLOCKSTR_M = 0x2C0D, // measurement: clock stretching, medium repeatability 
  CMD_MEAS_CLOCKSTR_L = 0x2C10, // measurement: clock stretching, low repeatability 
  CMD_MEAS_POLLING_H  = 0x2400, // measurement: polling, high repeatability 
  CMD_MEAS_POLLING_M  = 0x240B, // measurement: polling, medium repeatability 
  CMD_MEAS_POLLING_L  = 0x2416, // measurement: polling, low repeatability 
  CMD_MEAS_PERI_05_H  = 0x2032, // measurement: periodic 0.5 mps, high repeatability 
  CMD_MEAS_PERI_05_M  = 0x2024, // measurement: periodic 0.5 mps, medium repeatability 
  CMD_MEAS_PERI_05_L  = 0x202F, // measurement: periodic 0.5 mps, low repeatability 
  CMD_MEAS_PERI_1_H   = 0x2130, // measurement: periodic 1 mps, high repeatability 
  CMD_MEAS_PERI_1_M   = 0x2126, // measurement: periodic 1 mps, medium repeatability 
  CMD_MEAS_PERI_1_L   = 0x212D, // measurement: periodic 1 mps, low repeatability 
  CMD_MEAS_PERI_2_H   = 0x2236, // measurement: periodic 2 mps, high repeatability 
  CMD_MEAS_PERI_2_M   = 0x2220, // measurement: periodic 2 mps, medium repeatability 
  CMD_MEAS_PERI_2_L   = 0x222B, // measurement: periodic 2 mps, low repeatability 
  CMD_MEAS_PERI_4_H   = 0x2334, // measurement: periodic 4 mps, high repeatability 
  CMD_MEAS_PERI_4_M   = 0x2322, // measurement: periodic 4 mps, medium repeatability 
  CMD_MEAS_PERI_4_L   = 0x2329, // measurement: periodic 4 mps, low repeatability 
  CMD_MEAS_PERI_10_H  = 0x2737, // measurement: periodic 10 mps, high repeatability 
  CMD_MEAS_PERI_10_M  = 0x2721, // measurement: periodic 10 mps, medium repeatability 
  CMD_MEAS_PERI_10_L  = 0x272A, // measurement: periodic 10 mps, low repeatability 
  CMD_FETCH_DATA      = 0xE000, // readout measurements for periodic mode 
  CMD_R_AL_LIM_LS     = 0xE102, // read alert limits, low set 
  CMD_R_AL_LIM_LC     = 0xE109, // read alert limits, low clear 
  CMD_R_AL_LIM_HS     = 0xE11F, // read alert limits, high set 
  CMD_R_AL_LIM_HC     = 0xE114, // read alert limits, high clear 
  CMD_W_AL_LIM_HS     = 0x611D, // write alert limits, high set 
  CMD_W_AL_LIM_HC     = 0x6116, // write alert limits, high clear  
  CMD_W_AL_LIM_LC     = 0x610B, // write alert limits, low clear 
  CMD_W_AL_LIM_LS     = 0x6100, // write alert limits, low set 
  CMD_NO_SLEEP        = 0x303E, 
}etCommands; 
 
// Measurement Repeatability 
typedef enum
{ 
  REPEATAB_HIGH,   // high repeatability 
  REPEATAB_MEDIUM, // medium repeatability 
  REPEATAB_LOW,    // low repeatability 
}etRepeatability; 
 
// Measurement Mode 
typedef enum
{ 
  MODE_CLKSTRETCH, // clock stretching 
  MODE_POLLING,    // polling 
}etMode; 
 
typedef enum
{ 
  FREQUENCY_HZ5,  //  0.5 measurements per seconds 
  FREQUENCY_1HZ,  //  1.0 measurements per seconds 
  FREQUENCY_2HZ,  //  2.0 measurements per seconds 
  FREQUENCY_4HZ,  //  4.0 measurements per seconds 
  FREQUENCY_10HZ, // 10.0 measurements per seconds 
}etFrequency; 
// Error codes 
typedef enum
{ 
  NO_ERROR       = 0x00, // no error 
  ACK_ERROR      = 0x01, // no acknowledgment error 
  CHECKSUM_ERROR = 0x02, // checksum mismatch error 
  TIMEOUT_ERROR  = 0x04, // timeout error 
  PARM_ERROR     = 0x80, // parameter out of range error 
}etError; 

typedef union 
{ 
unsigned int i;
  float f;
} value;

enum {TEMP,HUMI};

typedef struct
{ 
 
 u16 Temper;
 u16 RHum;
} __attribute__((packed))ResultType;
#define IIC_SCL    PBout(6) //SCL
#define IIC_SDA    PBout(5) //SDA	 
#define READ_SDA   PBint(5)  //输入SDA 

#define ACK  1
#define NACK 0
#define DevAddr  0x44
//-- Defines ------------------------------------------------------------------ 
// Generator polynomial for CRC 
#define POLYNOMIAL  0x131 // P(x) = x^8 + x^5 + x^4 + 1 = 100110001 

							  
//////////////////////////////////////////////////////////////////////////////////
//IO方向设置
#define SDA_IN()  GPIO_InitTypeDef GPIO_InitStruct = {0};\
	                            GPIO_InitStruct.Pin = GPIO_PIN_5; \
                                GPIO_InitStruct.Mode= GPIO_MODE_INPUT; \
                                GPIO_InitStruct.Pull = GPIO_PULLUP; \
                                GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; \
                                HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
#define SDA_OUT() GPIO_InitTypeDef GPIO_InitStruct = {0};\
	                     GPIO_InitStruct.Pin = GPIO_PIN_5;\
	                     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;\
	                     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;\
	                     HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

//IO操作函数	 
